//构建数据   并    验证


const Validator = require('fastest-validator');

//通用验证放阿飞
function validate(data, rules) {
    return (new Validator()).validate(data, rules);
}

//痛点数据
module.exports.pain_points = class {
    constructor() {
        this.errno = false;
        this.errmsg = false;
        this.olddata = false;
        this.targetData = false;
    }

    build(data, is_pk = true) {
        this.is_pk = is_pk;
        if (!think.isObject(data)) {
            this.errno = 999;
            this.errmsg = '参数必须是对象'
            return this.builder();
        } else {
            this.olddata = data;
        }

        if ((this.verify()) === false) return this.builder();
        if ((this.init()) === false) return this.builder();
        return this.builder();

    }


    //传入参数验证
    verify() {
        let baseRules = {
            pk_pain_point: {type: "string", min: 1},
            opeater_id: {type: "string", min: 1},
            is_carousel: {type: "string", enum: ['true', 'false']},
            cover: {type: "string", min: 1, max: 255},
            title: {type: "string", min: 1},
            content: {type: "string", min: 1},
            end_time: {type: "number", min: -2},
            range_type: {type: "string", enum: ['public', 'class', 'other']},
            range_value: {type: "string"},
            is_open_solution: {type: "string", enum: ['open', 'unopen']},
            status: {type: "string", enum: ['able', 'deleted']},
            create_time: {type: "number"},
            update_time: {type: "number"},
            delete_time: {type: "number"},
            create_ip: {type: "string"},
            update_ip: {type: "string"},
            delete_ip: {type: "string"},


        };
        let vres = validate(this.olddata, baseRules);
        if (vres !== true) {
            this.errno = 999;
            this.errmsg = vres[0].message;
            return false
        } else {
            this.errno = 0;
            this.errmsg = '成功';
            return true;
        }

    }

    init() {
        if (this.is_pk) {
            this.targetData = {
                pk_pain_point: this.olddata['pk_pain_point'],
                opeater_id: this.olddata['opeater_id'],
                is_carousel: this.olddata['is_carousel'],
                cover: this.olddata['cover'],
                title: this.olddata['title'],
                content: this.olddata['content'],
                end_time: this.olddata['end_time'],
                range_type: this.olddata['range_type'],
                range_value: this.olddata['range_value'],
                is_open_solution: this.olddata['is_open_solution'],
                status: this.olddata['status'],
                create_time: this.olddata['create_time'],
                update_time: this.olddata['update_time'],
                delete_time: this.olddata['delete_time'],
                create_ip: this.olddata['create_ip'],
                update_ip: this.olddata['update_ip'],
                delete_ip: this.olddata['delete_ip'],
            }
        } else {
            this.targetData = {
                opeater_id: this.olddata['opeater_id'],
                is_carousel: this.olddata['is_carousel'],
                cover: this.olddata['cover'],
                title: this.olddata['title'],
                content: this.olddata['content'],
                end_time: this.olddata['end_time'],
                range_type: this.olddata['range_type'],
                range_value: this.olddata['range_value'],
                is_open_solution: this.olddata['is_open_solution'],
                status: this.olddata['status'],
                create_time: this.olddata['create_time'],
                update_time: this.olddata['update_time'],
                delete_time: this.olddata['delete_time'],
                create_ip: this.olddata['create_ip'],
                update_ip: this.olddata['update_ip'],
                delete_ip: this.olddata['delete_ip'],
            }
        }


    }

    builder() {
        if (this.errno !== false && this.errmsg !== false) {
            return {
                errno: this.errno,
                errmsg: this.errmsg,
                data: this.targetData
            }
        } else {
            return false;
        }

    }

};

//痛点修改数据
module.exports.pain_points_modify = class {
    constructor() {
        this.errno = false;
        this.errmsg = false;
        this.olddata = false;
        this.targetData = false;
    }

    build(data) {
        if (!think.isObject(data)) {
            this.errno = 999;
            this.errmsg = '参数必须是对象'
            return this.builder();
        } else {
            this.olddata = data;
        }

        if ((this.verify()) === false) return this.builder();
        if ((this.init()) === false) return this.builder();
        return this.builder();

    }


    //传入参数验证
    verify() {
        let baseRules = {
            pk_pain_point: {type: "string", min: 1},
            cover: {type: "string", min: 1, max: 255},
            title: {type: "string", min: 1},
            content: {type: "string", min: 1},
            end_time: {type: "number", min: -1},
            range_type: {type: "string", enum: ['public', 'class', 'other']},
            range_value: {type: "string"},

        };
        let vres = validate(this.olddata, baseRules);
        if (vres !== true) {
            this.errno = 999;
            this.errmsg = vres[0].message;
            return false
        } else {
            this.errno = 0;
            this.errmsg = '成功';
            return true;
        }

    }

    init() {
        this.targetData = this.olddata;
    }

    builder() {
        if (this.errno !== false && this.errmsg !== false) {
            return {
                errno: this.errno,
                errmsg: this.errmsg,
                data: this.targetData
            }
        } else {
            return false;
        }

    }

};

//痛点查询构建器
module.exports.pain_points_select_opion = class {
    constructor() {
        this.errno = false;
        this.errmsg = false;
        this.olddata = false;
        this.targetData = false;
    }

    build(data, is_pk = true) {
        this.is_pk = is_pk;
        if (!think.isObject(data)) {
            this.errno = 999;
            this.errmsg = '参数必须是对象'
            return this.builder();
        } else {
            this.olddata = data;
        }

        if ((this.verify()) === false) return this.builder();
        if ((this.init()) === false) return this.builder();
        return this.builder();

    }


    //传入参数验证
    verify() {
        let baseRules = {
            pages: {type: "number", min: 1},
            limit: {type: "number", min: 1},
            oredername: {type: "string", enum: ['end_time','create_time','update_time']},
            orderrule: {type: "string", enum: ['desc', 'asc']},
            period: {type: "string", enum: ['past', 'now', 'forever','none']},
            range_type:{type: "string", enum: ['class', 'public']}

        };
        let vres = validate(this.olddata, baseRules);
        if (vres !== true) {
            this.errno = 999;
            this.errmsg = vres[0].message;
            return false
        } else {
            this.errno = 0;
            this.errmsg = '成功';
            return true;
        }

    }

    init() {
        this.targetData = this.olddata;
    }

    builder() {
        if (this.errno !== false && this.errmsg !== false) {
            return {
                errno: this.errno,
                errmsg: this.errmsg,
                data: this.targetData
            }
        } else {
            return false;
        }

    }

};

//解决方案查询构建器
module.exports.solutions_option = class {
    constructor() {
        this.errno = false;
        this.errmsg = false;
        this.olddata = false;
        this.targetData = false;
    }

    build(data, is_pk = true) {
        this.is_pk = is_pk;
        if (!think.isObject(data)) {
            this.errno = 999;
            this.errmsg = '参数必须是对象'
            return this.builder();
        } else {
            this.olddata = data;
        }

        if ((this.verify()) === false) return this.builder();
        if ((this.init()) === false) return this.builder();
        return this.builder();

    }


    //传入参数验证
    verify() {
        let baseRules = {
            pages: {type: "number", min: 1},
            limit: {type: "number", min: 1},
            ordername: {type: "string", enum: ['score','create_time','praise','noscore']},
            orderrule: {type: "string", enum: ['desc', 'asc']},

        };
        let vres = validate(this.olddata, baseRules);
        if (vres !== true) {
            this.errno = 999;
            this.errmsg = vres[0].message;
            return false
        } else {
            this.errno = 0;
            this.errmsg = '成功';
            return true;
        }

    }

    init() {
        this.targetData = this.olddata;
    }

    builder() {
        if (this.errno !== false && this.errmsg !== false) {
            return {
                errno: this.errno,
                errmsg: this.errmsg,
                data: this.targetData
            }
        } else {
            return false;
        }

    }

};

//解决方案数据
module.exports.solutions = class {
    constructor() {
        this.errno = false;
        this.errmsg = false;
        this.olddata = false;
        this.targetData = false;
    }

    build(data, is_pk = true) {
        if (!think.isObject(data)) {
            this.errno = 999;
            this.errmsg = '参数必须是对象'
            return this.builder();
        } else {
            this.olddata = data;
        }

        if ((this.verify()) === false) return this.builder();
        if ((this.init()) === false) return this.builder();
        return this.builder();

    }


    //传入参数验证
    verify() {
        let baseRules = {
            pk_pain_point_solution: {type: "string", min: 1},
            fk_pain_point: {type: "string", min: 1},
            fk_user: {type: "string", min: 1},
            opeater_id: {type: "string", min: 1},
            score: {type: "number", min: 0},
            praise: {type: "number", min: 0},
            status: {type: "string", enum: ['able', 'deleted']},
            cover: {type: "string", min: 1, max: 255},
            title: {type: "string", min: 1},
            content: {type: "string", min: 1},
            isteam: {type: "string", enum: ['team', 'personal', 'other']},
            team_memember: {type: "string"},
            create_time: {type: "number"},
            update_time: {type: "number"},
            delete_time: {type: "number"},
            create_ip: {type: "string"},
            update_ip: {type: "string"},
            delete_ip: {type: "string"},


        };
        let vres = validate(this.olddata, baseRules);
        if (vres !== true) {
            this.errno = 999;
            this.errmsg = vres[0].message;
            return false
        } else {
            this.errno = 0;
            this.errmsg = '成功';
            return true;
        }

    }

    init() {
       this.targetData=this.olddata;

    }

    builder() {
        if (this.errno !== false && this.errmsg !== false) {
            return {
                errno: this.errno,
                errmsg: this.errmsg,
                data: this.targetData
            }
        } else {
            return false;
        }

    }

};

//解决方案数据修该构建
module.exports.solutions_modify = class {
    constructor() {
        this.errno = false;
        this.errmsg = false;
        this.olddata = false;
        this.targetData = false;
    }

    build(data, is_pk = true) {
        if (!think.isObject(data)) {
            this.errno = 999;
            this.errmsg = '参数必须是对象'
            return this.builder();
        } else {
            this.olddata = data;
        }

        if ((this.verify()) === false) return this.builder();
        if ((this.init()) === false) return this.builder();
        return this.builder();

    }


    //传入参数验证
    verify() {
        let baseRules = {
            pk_pain_point_solution: {type: "string", min: 1},
            cover: {type: "string", min: 1, max: 255},
            title: {type: "string", min: 1},
            content: {type: "string", min: 1},
            isteam: {type: "string", enum: ['team', 'personal', 'other']},
            team_memember: {type: "string"},
            opeater_id: {type: "string", min: 1},
            update_time: {type: "number"},
            update_ip: {type: "string"},
        };
        let vres = validate(this.olddata, baseRules);
        if (vres !== true) {
            this.errno = 999;
            this.errmsg = vres[0].message;
            return false
        } else {
            this.errno = 0;
            this.errmsg = '成功';
            return true;
        }

    }

    init() {
        this.targetData=this.olddata;

    }

    builder() {
        if (this.errno !== false && this.errmsg !== false) {
            return {
                errno: this.errno,
                errmsg: this.errmsg,
                data: this.targetData
            }
        } else {
            return false;
        }

    }

};



//退回原因    数据构建器
module.exports.solutions_back_reason = class {
    constructor() {
        this.errno = false;
        this.errmsg = false;
        this.olddata = false;
        this.targetData = false;
    }

    build(data, is_pk = true) {
        if (!think.isObject(data)) {
            this.errno = 999;
            this.errmsg = '参数必须是对象';
            return this.builder();
        } else {
            this.olddata = data;
        }

        if ((this.verify()) === false) return this.builder();
        if ((this.init()) === false) return this.builder();
        return this.builder();

    }


    //传入参数验证
    verify() {
        let baseRules = {
            update_time: {type: "number"},
            update_ip: {type: "string"},
            status: {type: "string",enum: ['able', 'deleted', 'backed']},
            backed_reason: {type: "string", min: 1},

        };
        let vres = validate(this.olddata, baseRules);
        if (vres !== true) {
            this.errno = 999;
            this.errmsg = vres[0].message;
            return false
        } else {
            this.errno = 0;
            this.errmsg = '成功';
            return true;
        }

    }

    init() {
        this.targetData=this.olddata;

    }

    builder() {
        if (this.errno !== false && this.errmsg !== false) {
            return {
                errno: this.errno,
                errmsg: this.errmsg,
                data: this.targetData
            }
        } else {
            return false;
        }

    }

};